package com.crm.web.controller.clue;

import com.crm.business.service.BusinessService;
import com.crm.common.constant.ClueConstants;
import com.crm.common.core.controller.BaseController;
import com.crm.common.core.domain.AjaxResult;
import com.crm.common.domain.vo.AssignMentVo;
import com.crm.domain.Clue;
import com.crm.domain.FalseClue;
import com.crm.domain.dto.GainDto;
import com.crm.domain.dto.PageAndTimeClueDto;
import com.crm.service.IClueService;
import com.crm.common.core.page.TableDataInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/clues/clue")
@Slf4j
public class ClueController extends BaseController {

    @Autowired
    private IClueService clueService;

    /**
     * 分页条件查询线索列表
     * @param pageAndTimeClueDto
     * @return
     */
    @GetMapping("list")
    public TableDataInfo success(PageAndTimeClueDto pageAndTimeClueDto) {
        log.info("查询线索列表接收到的参数:{}",pageAndTimeClueDto);
        startPage();
        List<Clue> clueList = clueService.pageWithCreatTime(pageAndTimeClueDto);
        return getDataTable(clueList);
    }

    /**
     * 新增线索
     * @param clue
     * @return
     */
    @PostMapping
    public AjaxResult success(@RequestBody Clue clue) {
        log.info("新增线索接收到的参数:{}",clue);
        clueService.add(clue);
        return AjaxResult.success();
    }

    /**
     * 根据id查询线索
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult success(@PathVariable Long id) {
        log.info("查询线索接收到的参数:{}",id);
        Clue clue = clueService.getById(id);
        return AjaxResult.success(clue);
    }

    /**
     * 添加伪线索记录
     * @param id
     * @param falseClue
     * @return
     */
    @PutMapping("false/{id}")
    public AjaxResult addFalseClue(@PathVariable Long id,@RequestBody FalseClue falseClue) {
        log.info("添加线索接收到的参数:{}",falseClue);
        clueService.addFalseClue(id,falseClue);
        return AjaxResult.success();
    }

    /**
     * 线索池列表
     * @param pageAndTimeClueDto
     * @return
     */
    @GetMapping("pool")
    public TableDataInfo cluePoolList(PageAndTimeClueDto pageAndTimeClueDto){
        log.info("查询线索池列表接收到的参数:{}",pageAndTimeClueDto);
        startPage();
        // 给pageAndTimeClueDto设置status为3，表示查询线索池
        pageAndTimeClueDto.setStatus(ClueConstants.POOL);
        List<Clue> clueList = clueService.pageWithCreatTime(pageAndTimeClueDto);
        return getDataTable(clueList);
    }

    /**
     * 从公海池捞取线索
     * @param gainDto
     * @return
     */
    @PutMapping("gain")
    public AjaxResult gainClue(@RequestBody GainDto gainDto){
        log.info("领取线索接收到的参数:{}",gainDto);
        clueService.scooping(gainDto);
        return AjaxResult.success();
    }

    /**
     * 线索转商机
     * @param id
     * @return
     */
    @PutMapping("changeBusiness/{id}")
    public AjaxResult changeBusiness(@PathVariable Long id){
        log.info("转商机接收到的参数:{}",id);
        clueService.changeBusiness(id);
        return AjaxResult.success();
    }

    /**
     * 线索分配
     *
     * @param assignMentVo
     * @return
     */
    @PutMapping("/assignment")
    public AjaxResult assignment(@RequestBody AssignMentVo assignMentVo) {
        log.info("线索管理分配前端传来的参数是,客户id:{},客服id:{}", assignMentVo.getIds(), assignMentVo.getUserId());
        clueService.assignment(assignMentVo);
        return AjaxResult.success();
    }
}
